package com.cfjjc.gz.usraccount.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.cfjjc.gz.common.consts.SystemConst;
import com.cfjjc.gz.common.consts.TypeValueConst;
import com.cfjjc.gz.common.dao.DefaultDAO;
import com.cfjjc.gz.common.exception.OTSException;
import com.cfjjc.gz.common.service.BaseService;
import com.cfjjc.gz.common.util.DateTimeUtil;
import com.cfjjc.gz.common.util.RandomGUID;
import com.cfjjc.gz.common.util.paged.PagedList;
import com.cfjjc.gz.usraccount.vo.A_ORG_BONUSVO;
import com.cfjjc.gz.usraccount.vo.A_USRVO;
import static com.opensymphony.xwork2.Action.ERROR;
import static com.opensymphony.xwork2.Action.NONE;


import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 
 * 说明：机构信息表 业务逻辑层对象
 * 
 * @author liuxun
 * @since 2012-03-06
 * @version 1.0
 */
@Service
@Transactional(rollbackFor = Throwable.class)
public class A_ORG_BONUSService extends BaseService {

	/**
	 * logger对象
	 */
	private static final Logger logger = Logger.getLogger(A_ORG_BONUSService.class);

	@Autowired
	private DefaultDAO dao;

	// 要访问的dao的命名空间
	private String namespace = "A_ORG_BONUS";

	public A_ORG_BONUSService() {
		
		super();
		
	}
	
	public void queryA_USRs(A_ORG_BONUSVO param, PagedList records)
			throws OTSException {
		dao.queryPagedList(param, records, this.namespace);
	}

	/**
	 * 仅查询单条机构信息表的详细信息
	 */
	public A_ORG_BONUSVO queryA_ORG(A_ORG_BONUSVO param) throws OTSException {
		return (A_ORG_BONUSVO) dao.queryObject(param, this.namespace);
	}

	/**
	 * 根据条件检索机构信息表，返回分页结果
	 */
	public void queryA_ORGs(A_ORG_BONUSVO param, PagedList records)
			throws OTSException {
		dao.queryPagedList(param, records, this.namespace);
	}

	/**
	 * 根据条件检索机构信息表，返回所有结果
	 */
	public List<A_ORG_BONUSVO> queryA_ORGList(A_ORG_BONUSVO param) throws OTSException {
		return dao.queryList(param, this.namespace);
	}

	/**
	 * 增加机构信息表
	 */
	public void doInsertA_ORG(A_ORG_BONUSVO param) throws OTSException {

		param.setKey(param.getOrg_code());
	
//		_checkFields(param, SystemConst.OPERATION_TYPE_NEW);


		dao.insert(param, this.namespace);
		if (logger.isDebugEnabled()) {
			logger.debug("增加一条机构信息表记录:" + param);
		}

	}

	/**
	 * 修改机构信息表
	 */
	public void doUpdateA_ORG(A_ORG_BONUSVO param) throws OTSException {

		A_ORG_BONUSVO query = new A_ORG_BONUSVO();
		query.setOrg_code(param.getOrg_code());
		A_ORG_BONUSVO res = (A_ORG_BONUSVO) this.queryA_ORG(query);

		if (res == null) {
			throw new OTSException("要操作的记录不存在,请刷新页面后重试或联系管理员！");
		}
		param.setLast_extraction_data(DateTimeUtil.getDateTime());

		dao.update(param, this.namespace);
		if (logger.isDebugEnabled()) {
			logger.debug("修改机构信息表:" + param);
		}

	}
	
	
	public void doPrizesToUser(A_ORG_BONUSVO param) throws OTSException {
 
		A_ORG_BONUSVO query = new A_ORG_BONUSVO();
		query.setOrg_code(param.getOrg_code());
		A_ORG_BONUSVO res = (A_ORG_BONUSVO) this.queryA_ORG(query);

		if (res == null) {
			throw new OTSException("要操作的记录不存在,请刷新页面后重试或联系管理员！");
		}

		 /**
		  *寒凝，这里逻辑给处理下。状态，还有计算的钱。包括最后清算的时间和金钱等字段。
		  *begin
		  */
                  res.setLastlast_extraction_bonus(res.getLast_extraction_bonus());
                  res.setLast_extraction_bonus(res.getRemaining_bonus());
//                  res.setExtraction_time(DateTimeUtil.getDateTime());
                res.setRemaining_bonus("0");
                String extraction_time=res.getExtraction_time();
                if(extraction_time==null||extraction_time.equals(""))
                {
                  res.setExtraction_time("1");
                }
                else{
                 
                 res.setExtraction_time(Integer.parseInt(extraction_time)+1+"");
                }
                 res.setLast_extraction_data(DateTimeUtil.getDateTime());
             
		  res.setIfclear("0");
		
		 /**
		  *end
		  */
          
		dao.update(res, this.namespace);
		if (logger.isDebugEnabled()) {
			logger.debug("已经给用户发完奖金:" + param);
		}
		
 
	}

           
// 批量发放奖金
 	public String doPrizes() throws Exception {
 		try {
 			
                        A_ORG_BONUSVO query = new A_ORG_BONUSVO();
                       List<A_ORG_BONUSVO> list= this.queryA_ORGList(query);
 			for (int n = 0; n < list.size(); n++) {
 				A_ORG_BONUSVO ao =list.get(n);
 				
 				this.doPrizesToUser(ao);
 				System.out.println("end doPrizes");
 			}
 		} catch (OTSException e) {
 			
 			return ERROR;
 		}
 		return NONE;
 	}
	 

}